/**
 * @author Nicolas Latorre
 */
package sa4.form

import scala.collection.mutable.HashMap
import scala.xml._
import javax.servlet.http._
import sa4.routing.Routing

class Form(val tags: List[Tag]) {
  
  def this() = this(List())

  def makeForm = {
    var attr = ""
    tags.head.attributes.map(x => attr += " " + x._1 + "=\"" + x._2 + "\"")
    var html = "<form " + attr + ">"
    for (tag <- tags.tail) {
      html += "<p><span>" + tag.span + "</span>"
      html += tag.makeNode
      html += "</p>"
      //if (tag.newline) html += "<br/>"
    }
    html += "</form>"
    XML.loadString(html)
  }
  
  def getAttribute(attribute: String): String = {
    for(at <- tags.head.attributes) {
      at._1 match {
        case attribute => return at._2
      }
    }
    return ""
  }
}

class Tag(val tag: String, val attributes: List[(String, String)], val span: String) {

  def makeNode = {
    var attris = ""
    for (attribute <- attributes.map(x => " " + x._1 + "=\"" + x._2 + "\"")) {
      attris += attribute
    }
    XML.loadString("<" + tag + attris + " />")
  }
}